#include <iostream>
#include <pthread.h>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include "RingQueue.hpp"
#include "Task.hpp"
std::string SelfName()
{
    char name[128];
    snprintf(name,sizeof(name),"thread:0X%x",pthread_self());
    return name;
}
void* ProductorRoutine(void* product)
{
    RingQueue<Task>* rqueue=static_cast<RingQueue<Task>*>(product);
    while(1)
    {
        //模拟构建一个任务
        int x=rand()%1000;
        int y=rand()%2000;
        char op=oper[rand()%oper.size()];
        Task t(x,y,op,myMath);//构建任务对象
        rqueue->Push(t);
        std::cout<<SelfName()<<"生产者生产任务成功:"<<t.toTaskString()<<std::endl;
        sleep(1);
    }
}
void* ConsumerRoutine(void* consume)
{
    RingQueue<Task>* rqueue=static_cast<RingQueue<Task>*>(consume);
    while(1)
    {
        Task t;
        rqueue->Pop(&t);
        std::cout<<SelfName()<<"消费者消费任务成功"<<t()<<std::endl;
    }
}
int main()
{
    srand((unsigned int)time(nullptr)^getpid()^pthread_self());


    RingQueue<Task>* rq=new RingQueue<Task>();
    pthread_t p[4],c[7];//定义生产者消费者线程
    for(int i=0;i<4;++i)
        pthread_create(p+i,nullptr,ProductorRoutine,rq);
    for(int i=0;i<7;++i)
        pthread_create(c+i,nullptr,ConsumerRoutine,rq);
    
    for(int i=0;i<4;++i)
        pthread_join(p[i],nullptr);
    for(int i=0;i<7;++i)
        pthread_join(c[i],nullptr);
    return 0;
}